package com.sgcc.fj.pmos.mobileapp.fragment;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.lxj.xpopup.XPopup;
import com.sgcc.fj.pmos.common.base.MvpBaseFragment;
import com.sgcc.fj.pmos.common.bean.ImageInfoBean;
import com.sgcc.fj.pmos.common.utils.GlideUtil;
import com.sgcc.fj.pmos.common.utils.XXPermissionsUtil;
import com.sgcc.fj.pmos.common.utils.picture.PictureSelectUtil;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.mobileapp.activity.AssociatedEnterpriseInfoActivity;
import com.sgcc.fj.pmos.mobileapp.bean.DataDifferentItemBean;
import com.sgcc.fj.pmos.mobileapp.contract.BankInfoContract;
import com.sgcc.fj.pmos.mobileapp.presenter.BankInfoPresenter;
import com.sgcc.fj.pmos.mobileapp.utils.DataDifferentDialog;
import com.sgcc.fj.pmos.mobileapp.utils.OnNextListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author ChaoAJ
 * @describe 关联企业信息--银行信息
 * @time 2022/10/18 11:56
 */
public class BankInfoFragment extends MvpBaseFragment<BankInfoPresenter> implements BankInfoContract.View, View.OnClickListener {


    private ImageView imgSelectImg;
    private EditText etBank;
    private EditText etBankName;
    private EditText etBankNumber;
    private LinearLayout llImgBtnLayout;
    private TextView tvSee;
    private TextView tvReUpload;
    private TextView tvDelete;


    private String bankInfoPath;


    private ImageInfoBean.Info info;//接口返回的开户行信息


    private OnNextListener onNextListener;


    public static BankInfoFragment newInstance() {
        BankInfoFragment fragment = new BankInfoFragment();
        return fragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_back_info;
    }


    @Override
    protected BankInfoPresenter createPresenter() {
        return new BankInfoPresenter();
    }

    @Override
    protected void initViews() {
        super.initViews();
        imgSelectImg = mRootView.findViewById(R.id.imgSelectImg);
        etBank = mRootView.findViewById(R.id.etBank);
        etBankName = mRootView.findViewById(R.id.etBankName);
        etBankNumber = mRootView.findViewById(R.id.etBankNumber);


        llImgBtnLayout = mRootView.findViewById(R.id.llImgBtnLayout);
        tvSee = mRootView.findViewById(R.id.tvSee);
        tvReUpload = mRootView.findViewById(R.id.tvReUpload);
        tvDelete = mRootView.findViewById(R.id.tvDelete);


    }

    @Override
    protected void initData() {
        super.initData();

        imgSelectImg.setOnClickListener(this);
        tvSee.setOnClickListener(this);
        tvReUpload.setOnClickListener(this);
        tvDelete.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        if (id == R.id.imgSelectImg || id == R.id.tvReUpload) {
            XXPermissionsUtil.camera(getActivity(), new XXPermissionsUtil.PermissionCallback() {
                @Override
                public void Call() {
                    PictureSelectUtil.select(BankInfoFragment.this, 0x24, true, null);
                }
            });
        } else if (id == R.id.tvSee) {

        } else if (id == R.id.tvDelete) {
            llImgBtnLayout.setVisibility(View.GONE);
            imgSelectImg.setImageResource(R.mipmap.ic_bank_info);
            etBank.setText("");
            etBankName.setText("");
            etBankNumber.setText("");

        }

//        switch (id) {
//            case R.id.imgSelectImg:
//            case R.id.tvReUpload:
//                XXPermissionsUtil.camera(getActivity(), new XXPermissionsUtil.PermissionCallback() {
//                    @Override
//                    public void Call() {
//                        PictureSelectUtil.select(BankInfoFragment.this, 0x24, true, null);
//                    }
//                });
//                break;
//            case R.id.tvSee:
//                break;
//            case R.id.tvDelete:
//                llImgBtnLayout.setVisibility(View.GONE);
//                imgSelectImg.setImageResource(R.mipmap.ic_bank_info);
//                etBank.setText("");
//                etBankName.setText("");
//                etBankNumber.setText("");
//
//
//                break;
//
//        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 0x24) {
                ArrayList<LocalMedia> localMediaList = PictureSelector.obtainSelectorList(data);
                if (null != localMediaList && localMediaList.size() > 0) {
                    bankInfoPath = PictureSelectUtil.getPath(localMediaList.get(0));
                    GlideUtil.loadImg(bankInfoPath, imgSelectImg);
                    mPresenter.uploadFile(bankInfoPath);
                }
            }
        }


    }

    @Override
    public void uploadFileSuccess(ImageInfoBean.Info info) {
        this.info = info;
//        llImgBtnLayout.setVisibility(View.VISIBLE);
        etBank.setText(info.getDepositBank());
        etBankName.setText(info.getDepositName());
        etBankNumber.setText(info.getAccount());
    }

    @Override
    public void saveDataSuccess(String text) {
        mPresenter.commitRegInfo();
    }

    @Override
    public void commitRegInfoSuccess() {
        ((AssociatedEnterpriseInfoActivity) getActivity()).submitSuccess(10);
    }

    @Override
    public void onFailed(int code) {
        if (code == 0) {
            ToastUtils.showShort("文件上传失败");
        }
        if (code == 1) {
            ToastUtils.showShort("文件解析失败,请更换文件");
        }

    }


    public void next() {
        String bank = etBank.getText().toString().trim();//开户银行
        String bankName = etBankName.getText().toString().trim();//开户名称
        String bankNumber = etBankNumber.getText().toString().trim();//开户账户
        List<DataDifferentItemBean> dataDifferentItemBeanList = new ArrayList<>();
        //判断输入的和识别的是否一致
        if (null != info) {
            if (!info.getDepositBank().equals(bank)) {
                dataDifferentItemBeanList.add(new
                        DataDifferentItemBean("开户银行", bank, info.getDepositBank()));
            }

            if (!info.getDepositName().equals(bankName)) {
                dataDifferentItemBeanList.add(new
                        DataDifferentItemBean("开户名称", bankName, info.getDepositName()));
            }
            if (!info.getAccount().equals(bankNumber)) {
                dataDifferentItemBeanList.add(new
                        DataDifferentItemBean("开户账户", bankNumber, info.getAccount()));
            }
        }

        if (dataDifferentItemBeanList.size() > 0) {
            new XPopup.Builder(getContext())
                    .isDestroyOnDismiss(true)
                    .isViewMode(true)
                    .asCustom(new DataDifferentDialog(getContext(), "以下信息与系统识别信息不一致，请确认是否无误",
                            dataDifferentItemBeanList
                            , new DataDifferentDialog.OnConfirmClickListener() {
                        @Override
                        public void onConfirm() {
                            save(bank, bankName, bankNumber);
                        }
                    }))
                    .show();
        } else {
            save(bank, bankName, bankNumber);
        }


    }

    public void save(String bank, String bankName, String bankNumber) {
//      {
//"depositBank": "2222222", //开户银行
//"depositName": "称江苏恒力新材料科技有限公司", //开户名称
//"account": "91320509MA213G5P8M" //开户账户
//}
        Map<String, Object> params = new HashMap<>();
        params.put("depositBank", bank);
        params.put("depositName", bankName);
        params.put("account", bankNumber);
        mPresenter.saveData(params);

    }


    public void submit() {
        mPresenter.commitRegInfo();
    }

}